#!/usr/bin/env python
# -*- coding: utf-8 -*-

import gtk

def open_file_dialog(parent):
    """
    打开文件 文件选择对话框
    @param parent:父控件
    @type parent:gtk.Widget
    @return:(gtk.RESPONSE_CANCEL or gtk.RESPONSE_OK) and 文件绝对路径
    @rtype:gtk response ID, string
    """
    file_dialog = gtk.FileChooserDialog(title='打开文件',
                                        parent=parent,
                                        action=gtk.FILE_CHOOSER_ACTION_OPEN,
                                        buttons = ( gtk.STOCK_CANCEL,
                                                    gtk.RESPONSE_CANCEL,
                                                    gtk.STOCK_OK,
                                                    gtk.RESPONSE_OK),
                                        backend=None)
    res = file_dialog.run()
    abs_filename = file_dialog.get_filename()
    file_dialog.destroy()
    return (res, abs_filename)

def save_file_dialog(parent):
    """
    保存文件 文件选择对话框
    @param parent:父控件
    @type parent:gtk.Widget
    @return:(gtk.RESPONSE_CANCEL or gtk.RESPONSE_OK) and 文件绝对路径
    @rtype:gtk response ID, string
    """
    file_dialog = gtk.FileChooserDialog(title='选择文件',
                                        parent=parent,
                                        action=gtk.FILE_CHOOSER_ACTION_SAVE,
                                        buttons = ( gtk.STOCK_CANCEL,
                                                    gtk.RESPONSE_CANCEL,
                                                    gtk.STOCK_OK,
                                                    gtk.RESPONSE_OK),
                                        backend=None)
    res = file_dialog.run()
    abs_filename = file_dialog.get_filename()
    file_dialog.destroy()
    return (res, abs_filename)

def save_dialog(parent, editor):
    """
    确定文件是否要保存
    @param parent:父控件
    @type parent:gtk.Widget
    @return:gtk.RESPONSE_YES or gtk.RESPONSE_NO or gtk.RESPONSE_CANCEL
    @rtype:gtk response ID
    """
    save_dialog = gtk.Dialog(title = 'PyPad',
                            parent=parent,
                            flags = gtk.DIALOG_MODAL,
                            buttons = ( gtk.STOCK_YES,
                                        gtk.RESPONSE_YES,
                                        gtk.STOCK_NO,
                                        gtk.RESPONSE_NO,
                                        gtk.STOCK_CANCEL,
                                        gtk.RESPONSE_CANCEL)
                            )
    save_label = gtk.Label('文件已修改 是否保存为\'%s\'?' % editor.get_abs_filename())
    save_dialog.vbox.pack_start(save_label)
    save_label.show()
    res = save_dialog.run()
    save_dialog.destroy()
    return res